<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>FusionCharts v3 Documentation</title>
		<link rel="stylesheet" href="Style.css" type="text/css" />
	</head>
	<body>
		<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
			<tr>
				<td><h2 class="pageHeader">Using FusionCharts with ASP.NET  2.0 (VB.NET) &gt; Plotting data from a 
						database
					</h2>				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><p>In this section, we'll show you how to use 
						FusionCharts and ASP.NET to plot charts from data contained in a database. 
						We'll create a pie chart to show &quot;Production by Factory&quot; using:
					</p>
					<ul>
						<li>
							<span class="codeInline">dataXML</span>
							method first.</li>
						<li>
							Thereafter, we'll convert this chart to use
							<span class="codeInline">dataURL</span>
							method.						</li>
					</ul>
					<p>For the sake of ease, we'll use an Access Database. The database is present in
						<span class="codeInline">Download Package &gt; Code &gt; VB_NET &gt; DB </span>folder. 
						You can, however, use any database with FusionCharts including MS SQL, Oracle, 
						mySQL etc.					</p>
					<p><strong>Before you go further with this page, we recommend you to please see the 
							previous section &quot;Basic Examples&quot; as we start off from concepts 
							explained in that page. </strong>					</p>
					<p class="highlightBlock">The code examples contained in this page are present in
						<span class="codeInline">Download Package &gt; Code &gt; VB_NET &gt; DBExample </span>
						folder. The Access database is present in
						<span class="codeInline">Download Package &gt; Code &gt; VB_NET &gt;</span>
						<span class="codeInline">DB</span>.					</p>				</td>
			</tr>
			<tr>
				<td valign="top" class="text">&nbsp;</td>
			</tr>
			<tr>
				<td valign="top" class="header">Database Structure				</td>
			</tr>
			<tr>
				<td valign="top" class="text">Before we code the ASP.NET pages to retrieve data, 
					let's quickly have a look at the database structure.				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><img src="Images/Code_DB.gif" width="372" height="124" /></td>
			</tr>
			<tr>
				<td valign="top" class="text"><p>The database contains just 2 tables:</p>
					<ol>
						<li>
							<span class="codeInline">Factory_Master</span>: To store the name and id of 
							each factory</li>
						<li>
							<span class="codeInline">Factory_Output</span>: To store the number of units 
							produced by each factory for a given date.</li>
					</ol>
					<p>For demonstration, we've fed some dummy data in the database. Let's now shift 
						our attention to the ASP.NET page that will interact with the database, fetch 
						data and then render a chart.					</p>				</td>
			</tr>
			<tr>
				<td valign="top" class="text">&nbsp;</td>
			</tr>
			<tr>
				<td valign="top" class="header">Building the ASP.NET Page for dataXML Method				</td>
			</tr>
			<tr>
				<td valign="top" class="text">The ASP.NET page for
					<span class="codeInline">dataXML</span>
					method example is named as
					<span class="codeInline">BasicDBExample.aspx</span>
					(in
					<span class="codeInline">DBExample</span>
					folder). It contains the following code (<span class="codeInline">GetFactorySummaryChartHtml()</span> method from code behind page also reproduced below): </td>
			</tr>
			<tr>
				<td valign="top" class="codeBlock"><p>&lt;%@ Page Language=&quot;VB&quot; AutoEventWireup=&quot;false&quot; CodeFile=&quot;BasicDBExample.aspx.vb&quot;     Inherits=&quot;DBExample_BasicDBExample&quot; %&gt;<br />
				  &lt;HTML&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;HEAD&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;TITLE&gt;FusionCharts - Database Example &lt;/TITLE&gt;<br />
&nbsp;&nbsp;&nbsp;<strong>&nbsp;&nbsp;&nbsp;&lt;SCRIPT LANGUAGE=&quot;Javascript&quot; SRC=&quot;../FusionCharts/FusionCharts.js&quot;&gt;&lt;/SCRIPT&gt;</strong><br />
&nbsp;&nbsp;&nbsp;&lt;/HEAD&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;body&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form id='form1' name='form1' method='post' runat=&quot;server&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>&nbsp;&nbsp;&lt;%=GetFactorySummaryChartHtml()%&gt;</strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;/body&gt;<br />
&lt;/HTML&gt;<br />
<br>
						<br>
						<br>
					</p>				</td>
			</tr>
			
			<tr>
			  <td valign="top" class="codeBlock"><strong><u>Code Behind page:</u></strong><br />
			      <br />
		        Imports InfoSoftGlobal<br />
			    Imports System.Text<br />
			    Imports DataConnection<br />
			    <br />
			    Partial Class DBExample_BasicDBExample<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>Inherits System.Web.UI.Page<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>Public Function <strong>GetFactorySummaryChartHtml</strong>() As String<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;'In this example, we show how to connect FusionCharts to a database.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'For the sake of ease, we've used an Access database which is present in<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'../DB/FactoryDB.mdb. It just contains two tables, which are linked to 
						each<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'other. <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'xmlData will be used to store the entire XML document generated</span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Dim xmlData As New StringBuilder()<br />
<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;'Generate the chart element</span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;xmlData.Append(&quot;&lt;chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units'&gt;&quot;)<br />
<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">'create recordset to get details for the factories</span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Dim factoryQuery As String = &quot;select a.FactoryId, a.FactoryName, sum(b.Quantity) as TotQ from .Factory_Master a, Factory_Output b where a.FactoryId=b.FactoryID group by a.FactoryId, a.FactoryName&quot;<br />
<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;'<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Dim oRs As New DbConn(factoryQuery)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;'Iterate through each record</span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;While oRs.ReadData.Read()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment">'Generate &lt;set name='..' value='..' /&gt; </span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlData.Append(&quot;&lt;set label='&quot; &amp; oRs.ReadData(&quot;FactoryName&quot;).ToString() &amp; &quot;' value='&quot; &amp; oRs.ReadData(&quot;TotQ&quot;).ToString &amp; &quot;' /&gt;&quot;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End While<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment">'</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>oRs.ReadData.Close()<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment">'Close chart element</span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;xmlData = (xmlData &amp;&quot;&lt;/chart&gt;&quot;)<br />
<br />
&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Create the chart - Pie 3D Chart with data from xmlData </span><br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;<strong>&nbsp;Return 
						InfoSoftGlobal.FusionCharts.RenderChart(&quot;../FusionCharts/Pie3D.swf&quot;, &quot;&quot;, 
						xmlData, &quot;FactorySum&quot;, &quot;600&quot;, &quot;300&quot;, False, False)</strong><br />
						<br />
&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;</span>End Function<br />
End Class</td>
		  </tr>
			<tr>
			  <td valign="top" class="text">&nbsp;</td>
		  </tr>
			<tr>
				<td valign="top" class="text"><p>The following actions are taking place in this code:</p>
					<ol>
						<li>We first include DataConnection and InfoSoftGlobal namespace. <a href="#DataConn">DataConnection</a> contains the   connection parameters to connect to  database.</li>
						<li>Next, we include
							<span class="codeInline">FusionCharts.js</span>
							JavaScript class to enable easy embedding of FusionCharts and call
							<span class="codeInline">GetFactorySummaryChartHtml()</span>
							method contained in code behind page.</li>
						<li>In  <span class="codeInline">GetFactorySummaryChartHtml()</span> function, 
							we then open a connection to database and retrieve the data as per the SQL passed.						</li>
						<li>
							Thereafter, we generate the XML data document by iterating through the 
							recordset and store it in
							<span class="codeInline">xmlData</span>
							variable.						</li>
						<li>
							Finally, we render the chart using
							<span class="codeInline">FusionCharts.RenderChart()</span>
							method and pass
							<span class="codeInline">xmlData</span>
							as
							<span class="codeInline">dataXML</span>.						</li>
					</ol>
					<p>When you now run the code, you'll get an output as under:					</p>				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><img src="Images/Code_DBOut.jpg" width="572" height="273" class="imageBorder" /></td>
			</tr>
			<tr>
				<td valign="top" class="text">&nbsp;</td>
			</tr>
			<tr>
				<td valign="top" class="header">Converting the example to use dataURL method				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><p>Let's now convert this example to use dataURL method. 
						As previously explained, in dataURL mode, you need two pages:</p>
					<ol>
						<li>
							<strong>Chart Container Page</strong> - The page which embeds the HTML code to 
							render the chart. This page also tells the chart where to load the data from. 
							We'll name this page as
							<span class="codeInline">Default.aspx</span>.						</li>
						<li>
							<strong>Data Provider Page</strong> - This page provides the XML data to the 
							chart. We'll name this page as
							<span class="codeInline">PieData.aspx</span></li>
					</ol>
					<p class="highlightBlock">The pages in this example are contained in<span class="codeInline"> Download Package &gt; Code &gt; VB_NET &gt; DB_dataURL</span>
						folder.					</p>				</td>
			</tr>
			<tr>
				<td valign="top" class="text">&nbsp;</td>
			</tr>
			<tr>
				<td valign="top" class="header">Chart Container Page -
					<span class="codeInline">Default.aspx </span></td>
			</tr>
			<tr>
				<td valign="top" class="text"><span class="codeInline">Default.aspx</span>
					contains the following code (with code behind page) to render the chart:				</td>
			</tr>
			<tr>
				<td valign="top" class="codeBlock">
					&lt;%@ Page Language=&quot;VB&quot; AutoEventWireup=&quot;false&quot; CodeFile=&quot;Default.aspx.vb&quot; Inherits=&quot;DB_dataURL_Default&quot; %&gt;<br>
					&lt;HTML&gt;<br>
					&nbsp;&lt;HEAD&gt;<br>
					&nbsp;&nbsp;&lt;TITLE&gt;FusionCharts - dataURL and Database Example 
					&lt;/TITLE&gt;<br>
					<strong>&nbsp;&nbsp;&lt;SCRIPT LANGUAGE="Javascript" 
					SRC="../FusionCharts/FusionCharts.js"&gt;&lt;/SCRIPT&gt;</strong><br>
					&nbsp;&lt;/HEAD&gt;<br>
					&nbsp;&lt;body&gt;<br>
					<strong>&nbsp;&nbsp;&nbsp;</strong>&lt;form id='form1' name='form1' method='post' runat=&quot;server&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>&lt;%=GetQuantityChartHtml()%&gt;</strong><br />
&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br>
					&nbsp;&lt;/body&gt;<br>
					&lt;/HTML&gt;<br>
					<br>
					<br>
					<strong>Public Function GetQuantityChartHtml() As String</strong><br>
					<span class="codeComment">&nbsp;'In this example, we show how to connect FusionCharts to a database <br>
&nbsp;'using dataURL method. In our other examples, we've used dataXML method<br>
&nbsp;'where the XML is generated in the same page as chart. Here, the XML data<br>
&nbsp;'for the chart would be generated in PieData.aspx.<br>
&nbsp;'To illustrate how to pass additional data as querystring to dataURL, <br>
&nbsp;'we've added an animate property, which will be passed to PieData.aspx. <br>
&nbsp;'PieData.aspx would handle this animate property and then generate the <br>
&nbsp;'XML accordingly.<br>
&nbsp;'For the sake of ease, we've used an Access database which is present in<br>
&nbsp;'../DB/FactoryDB.mdb. It just contains two tables, which are linked to 
					each<br>
&nbsp;'other.<br>
&nbsp;'Variable to contain dataURL<br>
&nbsp;'Set DataURL with animation property to 1<br>
&nbsp;'NOTE: It's necessary to encode the dataURL if you've added parameters to 
					it</span><br>
					&nbsp;Dim dataURL As String = 
					Server.UrlEncode(&quot;PieData.aspx?animate=1&quot;)<br>
					&nbsp;<span class="codeComment">'Create the chart - Pie 3D Chart with dataURL as strDataURL</span><br>
					<strong>Return FusionCharts.RenderChart(&quot;../FusionCharts/Pie3D.swf&quot;, dataURL, &quot;&quot;, &quot;FactorySum&quot;, &quot;600&quot;, &quot;300&quot;, False, False)</strong><br>
					End Function<br>				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><p>In the above code, we're:</p>
					<ol>
						<li>
							Including
							<span class="codeInline">FusionCharts.js</span>
							JavaScript class</li>
						<li>
							Create the
							<span class="codeInline">dataURL</span>
							string and store it in
							<span class="codeInline">dataURL</span>
							variable. We append a dummy propery
							<span class="codeInline">animate</span>
							to show how to pass parameters to
							<span class="codeInline">dataURL</span>. After building the
							<span class="codeInline">dataURL</span>, we encode it using
							<span class="codeInline">Server.UrlEncode</span>
							function.						</li>
						<li>
							Finally, we render the chart using
							<span class="codeInline">RenderChart()</span>
							method and set
							<span class="codeInline">dataURL variable </span>
							as
							<span class="codeInline">charts's dataURL</span>.						</li>
					</ol>				</td>
			</tr>
			<tr>
			  <td valign="top" class="header">&nbsp;</td>
		  </tr>
			<tr>
				<td valign="top" class="header">Creating the data provider page
					<span class="codeInline">PieData.aspx </span></td>
			</tr>
			<tr>
				<td valign="top" class="text">PieData.aspx contains the following code to output 
					XML Data:				</td>
			</tr>
			<tr>
				<td valign="top" class="codeBlock"><p>Imports DataConnection<br />
						Imports System.Text<br />
						Partial Class DB_dataURL_PieData<br />
						&nbsp;&nbsp;&nbsp;
Inherits System.Web.UI.Page<br />
<br />
				 &nbsp;&nbsp;&nbsp; Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br>
						&nbsp;<span class="codeComment">&nbsp;&nbsp;&nbsp;&nbsp; 'This page generates the XML data for the Pie Chart contained in<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;'Default.aspx. <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;'For the sake of ease, we've used an Access database which is 
						present in<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;'../DB/FactoryDB.mdb. It just contains two tables, which are linked 
						to each<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;'other. <br>
						</span><br />
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span class="codeComment">'xmlData will be used to store the entire XML document generated</span><br>
						&nbsp;&nbsp;&nbsp; &nbsp; Dim xmlData As New StringBuilder()<br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span class="codeComment">'Default.aspx has passed us a property animate. We request that.</span><br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dim animateChart As String<br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;animateChart = Request("animate")<br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span class="codeComment">'Set default value of 1</span><br>
						&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;If ((Not (animateChart) Is Nothing) AndAlso (animateChart.Length = 0)) Then<br>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; animateChart = "1"<br>
						&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;End If<br />
						<br>
						<span class="codeComment">&nbsp;&nbsp;&nbsp; &nbsp; 'Create the recordset to retrieve data<br />
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Generate the chart element</span><br>
						&nbsp;<span class="codeComment">&nbsp;&nbsp;&nbsp;&nbsp; </span>xmlData.Append(&quot;&lt;chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units' animation='&quot; &amp; animateChart &amp; &quot;'&gt;&quot;)<br />
						<br />
						&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment">'create recordset to get details for the factories</span><br />
						&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment"></span>Dim factoryQuery As String = &quot;select a.FactoryId, a.FactoryName, sum(b.Quantity) as TotQ from .Factory_Master a, Factory_Output b where a.FactoryId=b.FactoryID group by a.FactoryId, a.FactoryName&quot;<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="codeComment"></span>Dim oRs As New DbConn(factoryQuery)<br />
<br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;'Iterate through each factory</span><br>
						&nbsp;&nbsp; &nbsp; While oRs.ReadData.Read()<br>
						&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span class="codeInline">'Generate &lt;set name='..' value='..' /&gt;</span><br />
						&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; xmlData.Append(&quot;&lt;set label='&quot; &amp; oRs.ReadData(&quot;FactoryName&quot;).ToString() &amp; &quot;' value='&quot; &amp; oRs.ReadData(&quot;TotQ&quot;).ToString &amp; &quot;' /&gt;&quot;)<br>
						&nbsp;&nbsp; &nbsp;&nbsp;End While<br />
						<br>
						&nbsp;&nbsp; &nbsp;&nbsp;oRs.ReadData.Close()<br />
						<br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;'Finally, close &lt;chart&gt; element</span><br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;</span>xmlData = (xmlData + "&lt;/chart&gt;")<br>
						&nbsp;<span class="codeComment">&nbsp;<br />
						&nbsp;&nbsp; &nbsp;&nbsp;'Set Proper output content-type</span><br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;</span>Response.ContentType = "text/xml"<br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;</span><span class="codeComment">'Just write out the XML data<br>
&nbsp;&nbsp; &nbsp;&nbsp;'NOTE THAT THIS PAGE DOESN'T CONTAIN ANY HTML TAG, WHATSOEVER</span><br>
						&nbsp;&nbsp; &nbsp;<span class="codeComment">&nbsp;</span>Response.Write(xmlData)<br />
						<br>
						End Sub<br>
					</p>				</td>
			</tr>
			<tr>
				<td valign="top" class="text"><p>In the above page:</p>
					<ol>
						<li>
							We first request the animate property which has been passed to it (from
							<span class="codeInline">dataURL</span>)</li>
						<li>
							We generate the data and store it in
							<span class="codeInline">xmlData</span>
							variable</li>
						<li>
							Finally, we write this data to output stream without any HTML tags.						</li>
					</ol>
					<p>When you view this page, you'll get the same output as before.					</p>				</td>
			</tr>
			<tr>
			  <td valign="top" class="text">&nbsp;</td>
		  </tr>
			<tr>
			  <td valign="top" class="text"><a name="DataConn" class="header" id="DataConn">Inside DataConnection Namespace</a> </td>
		  </tr>
			<tr>
			  <td valign="top" class="text">We have used DataConnection Namespace in the above   code and in all subsequent Database examples. Using this class we establish connection to  the MS   Access database with ADO.NET component. Let's go through the lines of code   inside this class: </td>
		  </tr>
			<tr>
			  <td valign="top" class="text">&nbsp;</td>
		  </tr>
			<tr>
			  <td valign="top" class="codeBlock"><p>Imports Microsoft.VisualBasic<br />
			    Imports System.Data.Odbc<br />
			    Imports System.Data<br />
			    Imports System.Web<br />
			    Imports System.Configuration</p>
			    <p>Namespace DataConnection</p>
			    <p> <span class="codeComment">&nbsp;&nbsp; ''' &lt;summary&gt;<br />
&nbsp;&nbsp; ''' DataBase Connection Class.<br />
&nbsp;&nbsp; ''' &lt;/summary&gt;</span><br />
			      &nbsp;&nbsp; Public Class DbConn<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; Public connection As OdbcConnection<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; Public ReadData As OdbcDataReader<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; Public aCommand As OdbcCommand</p>
			    <p> <span class="codeComment">&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;summary&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' Data Connection and get Data Reader<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;/summary&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;param name=&quot;strQuery&quot;&gt;SQL Query&lt;/param&gt;</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; Public Sub New(ByVal strQuery As String)<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Dim ConnectionString As String, connectionName As String</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' MS Access DataBase Connection - Defined in Web.Config</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; connectionName = &quot;MSAccessConnection&quot;</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">'' SQL Server DataBase Connection - Defined in Web.Config<br />
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; '' connectionName = &quot;SQLServerConnection&quot;;</span></p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' Creating Connection string using web.config connection string</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; ConnectionString = ConfigurationManager.ConnectionStrings(connectionName).ConnectionString<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Try</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' Creating OdbcConnection Oject</span><br />
			       &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; connection = New OdbcConnection()</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' Setting Conection String</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; connection.ConnectionString = ConnectionString</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' Open Connection</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; connection.Open()</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' get reader</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; GetReader(strQuery)</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Catch ex As Exception<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; HttpContext.Current.Response.Write(ex.Message)<br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; End Try</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; End Sub<br />
                    <span class="codeComment"><br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;summary&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' Create an instance dataReader<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;/summary&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;param name=&quot;strQuery&quot;&gt;SQL Query&lt;/param&gt;<br />
&nbsp;&nbsp; &nbsp;&nbsp; ''' &lt;remarks&gt;Return type object of OdbcDataReader&lt;/remarks&gt;</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; Public Sub GetReader(ByVal strQuery As String)</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">'  Create a Command object</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; aCommand = New OdbcCommand(strQuery, connection)</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span class="codeComment">' Create data reader object using strQuery string</span><br />
			      &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; ReadData = aCommand.ExecuteReader(CommandBehavior.CloseConnection)</p>
			    <p> &nbsp;&nbsp; &nbsp;&nbsp;End Sub<br />
			      &nbsp;&nbsp; End Class<br />
			      End Namespace</p>
		      <p></p></td>
		  </tr>
			<tr>
			  <td valign="top" class="text">&nbsp;</td>
		  </tr>
			<tr>
			  <td valign="top" class="text">What it does:<br />
                <ul>
                  <li>Set up Connection as per the connection string defined in <span class="codeInline">web.config</span> file.<br />
                    <br />
                    <span class="codeInline">&lt;connectionStrings&gt;<br />
                    &nbsp;&nbsp;&nbsp; &lt;add name=&quot;MSAccessConnection&quot; providerName=&quot;System.Data.Odbc&quot; connectionString=&quot;Driver={Microsoft Access Driver (*.mdb)};Dbq=|DataDirectory|\FactoryDB.mdb&quot;/&gt;<br />
&lt;/connectionStrings&gt;</span><br />
<br />
                    <br />
                  To change your connection to any other database server, you only need to setup  <span class="codeInline">web.config</span> file. <br />
                  <br />
                  </li>
                  <li>The code to connect to SQL Server Database is also given in comment form. To   connect to SQL Server you have to activate the corresponding code. <br />
                  </li>
                  <li>It accepts SQL Query, executes it and returns the result as ASP.NET <span class="codeInline">DataReader</span> object -<span class="codeInline">ReadData</span><span class="text">. </span> </li>
              </ul></td>
		  </tr>
			
			<tr>
			  <td valign="top" class="text">&nbsp;</td>
		  </tr>
		</table>
	</body>
</html>
